package org.javaforever.gatescore.widgets;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontDropdown;
import org.javaforever.gatescore.core.FrontField;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.StatementList;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.exception.ValidateException;

public class ChildDatagrid extends Widget{
	protected String indexSuffix = "2";
	protected FrontDomain parentDomain;
	protected FrontDomain childDomain;
	protected FrontField parentId;
	
	public ChildDatagrid(FrontDomain parentDomain,FrontDomain childDomain,FrontField parentId,String indexSuffix) {
		super();
		this.parentDomain = parentDomain;
		this.childDomain = childDomain;
		this.parentId = parentId;
		this.indexSuffix = indexSuffix;
	}
	
	@Override
	public StatementList generateWidgetStatements() throws ValidateException{
		List<Writeable> sList = new ArrayList<>();
		long serial = 0L;
		sList.add(new Statement(serial+2500L,1,"<el-button-group>"));
		if (this.childDomain.getLanguage().equalsIgnoreCase("english")) {
			if (!this.childDomain.isVerbDenied("Add")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+3000L,2,"<el-button size=\"mini\" type=\"success\" icon=\"el-icon-plus\" @click=\"showAddDialog"+this.indexSuffix+"\">Add</el-button>"));
			if (!this.childDomain.isVerbDenied("View")) sList.add(new Statement(serial+3500L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"success\" icon=\"el-icon-search\" @click=\"showViewDialog"+this.indexSuffix+"\">View</el-button>"));
			if (!this.childDomain.isVerbDenied("Update")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+4000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"success\" icon=\"el-icon-edit\" @click=\"showEditDialog"+this.indexSuffix+"\">Update</el-button>"));
			if (!this.childDomain.isVerbDenied("Activate")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+5000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"primary\" icon=\"el-icon-star-off\" @click=\"activate"+this.childDomain.getCapFirstDomainName()+"\">Activate</el-button>"));
			if (!this.childDomain.isVerbDenied("Toggle")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+6000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"primary\" icon=\"el-icon-sort\" @click=\"toggle"+this.childDomain.getCapFirstDomainName()+"\">Toggle</el-button>"));
			if (!this.childDomain.isVerbDenied("ToggleOne")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+7000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"primary\" icon=\"el-icon-sort\" @click=\"toggleOne"+this.childDomain.getCapFirstDomainName()+"\">Toggle One</el-button>"));
			if (!this.childDomain.isVerbDenied("SoftDelete")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+8000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"primary\" icon=\"el-icon-delete\" @click=\"softDelete"+this.childDomain.getCapFirstDomainName()+"\">Soft Delete</el-button>"));
			if (!this.childDomain.isVerbDenied("Clone")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+8500L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"primary\" icon=\"el-icon-plus\" @click=\"clone"+this.childDomain.getCapFirstDomainName()+"\">Clone</el-button>"));
			if (!this.childDomain.isVerbDenied("Delete")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+9000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"danger\" icon=\"el-icon-delete\" @click=\"delete"+this.childDomain.getCapFirstDomainName()+"\">Delete</el-button>"));
			if (!this.childDomain.isVerbDenied("ActivateAll")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+10000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0\" size=\"mini\" type=\"primary\" icon=\"el-icon-star-off\" @click=\"activateAll"+this.childDomain.getCapFirstPlural()+"\">Batch Activate</el-button>"));
			if (!this.childDomain.isVerbDenied("SoftDeleteAll")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+11000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0\" size=\"mini\" type=\"primary\" icon=\"el-icon-delete\" @click=\"softDeleteAll"+this.childDomain.getCapFirstPlural()+"\">Batch Soft Delete</el-button>"));
			if (!this.childDomain.isVerbDenied("CloneAll")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+11500L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0\" size=\"mini\" type=\"primary\" icon=\"el-icon-plus\" @click=\"cloneAll"+this.childDomain.getCapFirstPlural()+"\">Batch Clone</el-button>"));
			if (!this.childDomain.isVerbDenied("DeleteAll")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+12000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0\" size=\"mini\" type=\"danger\" icon=\"el-icon-delete\" @click=\"deleteAll"+this.childDomain.getCapFirstPlural()+"\">Batch Delete</el-button>"));
			if (!this.childDomain.isVerbDenied("Export")) sList.add(new Statement(serial+13000L,2,"<el-button size=\"mini\" type=\"primary\" icon=\"el-icon-date\" @click=\"export"+this.childDomain.getCapFirstPlural()+"\">Excel</el-button>"));
			if (!this.childDomain.isVerbDenied("ExportPDF")) sList.add(new Statement(serial+14000L,2,"<el-button size=\"mini\" type=\"primary\" icon=\"el-icon-edit-outline\" @click=\"export"+this.childDomain.getCapFirstPlural()+"PDF\">PDF</el-button>"));
			if (!this.childDomain.isVerbDenied("ExportWord")) sList.add(new Statement(serial+15000L,2,"<el-button size=\"mini\" type=\"primary\" icon=\"el-icon-upload\" @click=\"export"+this.childDomain.getCapFirstPlural()+"Word\">Word</el-button>"));
			if (!this.childDomain.isVerbDenied("ExportPPT")) sList.add(new Statement(serial+15100L,2,"<el-button size=\"mini\" type=\"primary\" icon=\"el-icon-menu\" @click=\"exportPPT"+this.childDomain.getCapFirstPlural()+"\">PPT</el-button>"));
		}else {
			if (!this.childDomain.isVerbDenied("Add")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+3000L,2,"<el-button size=\"mini\" type=\"success\" icon=\"el-icon-plus\" @click=\"showAddDialog"+this.indexSuffix+"\">添加</el-button>"));
			if (!this.childDomain.isVerbDenied("View")) sList.add(new Statement(serial+3500L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"success\" icon=\"el-icon-search\" @click=\"showViewDialog"+this.indexSuffix+"\">查看</el-button>"));
			if (!this.childDomain.isVerbDenied("Update")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+4000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"success\" icon=\"el-icon-edit\" @click=\"showEditDialog"+this.indexSuffix+"\">编辑</el-button>"));
			if (!this.childDomain.isVerbDenied("Activate")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+5000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"primary\" icon=\"el-icon-star-off\" @click=\"activate"+this.childDomain.getCapFirstDomainName()+"\">激活</el-button>"));
			if (!this.childDomain.isVerbDenied("Toggle")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+6000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"primary\" icon=\"el-icon-sort\" @click=\"toggle"+this.childDomain.getCapFirstDomainName()+"\">切换</el-button>"));
			if (!this.childDomain.isVerbDenied("ToggleOne")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+7000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"primary\" icon=\"el-icon-sort\" @click=\"toggleOne"+this.childDomain.getCapFirstDomainName()+"\">留一切换</el-button>"));
			if (!this.childDomain.isVerbDenied("SoftDelete")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+8000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"primary\" icon=\"el-icon-delete\" @click=\"softDelete"+this.childDomain.getCapFirstDomainName()+"\">软删除</el-button>"));
			if (!this.childDomain.isVerbDenied("Clone")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+8500L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"primary\" icon=\"el-icon-plus\" @click=\"clone"+this.childDomain.getCapFirstDomainName()+"\">克隆</el-button>"));
			if (!this.childDomain.isVerbDenied("Delete")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+9000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0 || this.selectList"+this.indexSuffix+".length > 1\" size=\"mini\" type=\"danger\" icon=\"el-icon-delete\" @click=\"delete"+this.childDomain.getCapFirstDomainName()+"\">删除</el-button>"));
			if (!this.childDomain.isVerbDenied("ActivateAll")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+10000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0\" size=\"mini\" type=\"primary\" icon=\"el-icon-star-off\" @click=\"activateAll"+this.childDomain.getCapFirstPlural()+"\">批激活</el-button>"));
			if (!this.childDomain.isVerbDenied("SoftDeleteAll")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+11000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0\" size=\"mini\" type=\"primary\" icon=\"el-icon-delete\" @click=\"softDeleteAll"+this.childDomain.getCapFirstPlural()+"\">批软删除</el-button>"));
			if (!this.childDomain.isVerbDenied("CloneAll")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+11500L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0\" size=\"mini\" type=\"primary\" icon=\"el-icon-plus\" @click=\"cloneAll"+this.childDomain.getCapFirstPlural()+"\">批克隆</el-button>"));
			if (!this.childDomain.isVerbDenied("DeleteAll")&&!(this.childDomain instanceof org.javaforever.gatescore.core.FrontEnum)) sList.add(new Statement(serial+12000L,2,"<el-button :disabled=\"this.selectList"+this.indexSuffix+".length==0\" size=\"mini\" type=\"danger\" icon=\"el-icon-delete\" @click=\"deleteAll"+this.childDomain.getCapFirstPlural()+"\">批删除</el-button>"));
			if (!this.childDomain.isVerbDenied("Export")) sList.add(new Statement(serial+13000L,2,"<el-button size=\"mini\" type=\"primary\" icon=\"el-icon-date\" @click=\"export"+this.childDomain.getCapFirstPlural()+"\">Excel</el-button>"));
			if (!this.childDomain.isVerbDenied("ExportPDF")) sList.add(new Statement(serial+14000L,2,"<el-button size=\"mini\" type=\"primary\" icon=\"el-icon-edit-outline\" @click=\"export"+this.childDomain.getCapFirstPlural()+"PDF\">PDF</el-button>"));
			if (!this.childDomain.isVerbDenied("ExportWord")) sList.add(new Statement(serial+15000L,2,"<el-button size=\"mini\" type=\"primary\" icon=\"el-icon-upload\" @click=\"export"+this.childDomain.getCapFirstPlural()+"Word\">Word</el-button>"));
			if (!this.childDomain.isVerbDenied("ExportPPT")) sList.add(new Statement(serial+15100L,2,"<el-button size=\"mini\" type=\"primary\" icon=\"el-icon-menu\" @click=\"exportPPT"+this.childDomain.getCapFirstPlural()+"\">PPT</el-button>"));
		}
		sList.add(new Statement(serial+15500L,1,"</el-button-group>"));
		sList.add(new Statement(serial+16000L,0,"</el-form>"));
		sList.add(new Statement(serial+17000L,0,"<br>"));
		sList.add(new Statement(serial+19000L,0,"<el-table :data=\"list"+this.indexSuffix+"\" :header-cell-style=\"{'background-color': '#3b8fce','color':'white'}\" :row-style=\"selectedHighlight"+this.indexSuffix+"\" v-loading=\"listLoading"+this.indexSuffix+"\" ref=\"table"+this.indexSuffix+"\" border fit highlight-current-row style=\"width: 100%\" @select-all=\"handleCheckedAll"+this.indexSuffix+"\" @selection-change=\"handleChecked"+this.indexSuffix+"\">"));
		sList.add(new Statement(serial+22000L,1,"<el-table-column type=\"selection\" align=\"center\" width=\"40px\"/>"));
		FrontField fid = this.childDomain.getDomainId()==null?null: this.childDomain.getDomainId();
		if (fid != null ) {
			sList.add(new Statement(serial+25000L,1,"<el-table-column width=\"80px\" align=\"center\" label=\""+fid.getText()+"\">"));
			sList.add(new Statement(serial+26000L,2,"<template slot-scope=\"scope\">"));
			sList.add(new Statement(serial+27000L,3,"<span>{{ scope.row."+fid.getFieldName()+" }}</span>"));
			sList.add(new Statement(serial+28000L,2,"</template>"));
			sList.add(new Statement(serial+29000L,1,"</el-table-column>"));
		}
		serial = serial+31000L;
		for (FrontField f:this.childDomain.getAllFieldsWithoutId()) {
			if (!(f instanceof FrontDropdown)) {
			if (f.getFieldType()!=null&&f.getFieldType().getTypeName()!=null&&f.getFieldType().getTypeName().equalsIgnoreCase("image")) {
					sList.add(new Statement(serial,1,"<el-table-column :show-overflow-tooltip=\"true\" width=\"160px\" align=\"center\" label=\""+f.getText()+"\">"));
					sList.add(new Statement(serial+1000L,2,"<template slot-scope=\"scope\">"));
					sList.add(new Statement(serial+2000L,3,"<img v-if=\"!scope.row."+f.getLowerFirstFieldName()+"\" :src=\"'../static/images/blank.jpg'\" height=\"50\">"));
					sList.add(new Statement(serial+3000L,3,"<img v-else :src=\"'data:image/png;base64,'+scope.row."+f.getLowerFirstFieldName()+"\" height=\"50\">"));
					sList.add(new Statement(serial+4000L,2,"</template>"));
					sList.add(new Statement(serial+5000L,1,"</el-table-column>"));
				}else {
					sList.add(new Statement(serial,1,"<el-table-column width=\"160px\" align=\"center\" label=\""+f.getText()+"\">"));
					sList.add(new Statement(serial+1000L,2,"<template slot-scope=\"scope\">"));
					sList.add(new Statement(serial+2000L,3,"<span>{{ scope.row."+f.getFieldName()+" }}</span>"));
					sList.add(new Statement(serial+3000L,2,"</template>"));
					sList.add(new Statement(serial+4000L,1,"</el-table-column>"));
				}
				serial = serial + 6000L;
			}else {
				FrontDropdown dp = (FrontDropdown)f;
				sList.add(new Statement(serial,1,"<el-table-column width=\"160px\" align=\"center\" label=\""+dp.getText()+"\">"));
				sList.add(new Statement(serial+1000L,2,"<template slot-scope=\"scope\">"));
				sList.add(new Statement(serial+2000L,3,"{{ scope.row."+dp.getAliasName()+" | "+dp.getTarget().getLowerFirstDomainName()+"Filter }}"));
				sList.add(new Statement(serial+3000L,2,"</template>"));
				sList.add(new Statement(serial+4000L,1,"</el-table-column>"));
				serial = serial + 6000L;
			}			
		}
		sList.add(new Statement(serial,0,"</el-table>"));
		sList.add(new Statement(serial+2000L,0,"<pagination v-show=\"total"+this.indexSuffix+">0\" :total=\"total"+this.indexSuffix+"\" :page.sync=\"listQuery"+this.indexSuffix+".page\" :limit.sync=\"listQuery"+this.indexSuffix+".rows\" @pagination=\"search"+this.childDomain.getCapFirstPlural()+"ByFieldsByPage\" />"));

		StatementList sl = WriteableUtil.merge(sList);
		sl.setSerial(this.serial);
		return sl;
	}

	@Override
	public StatementList generateWidgetScriptStatements() throws ValidateException{
		try {	
			List<Writeable> sList = new ArrayList<>();
			long serial = 0L;

			StatementList sl = WriteableUtil.merge(sList);
			sl.setSerial(this.serial);
			return sl;
		}catch (Exception e) {
			e.printStackTrace();
			if (e instanceof ValidateException) throw (ValidateException)e;
			return null;
		}
	}

	@Override
	public boolean parse() {
		return true;
	}

	public FrontDomain getDomain() {
		return childDomain;
	}

	public void setDomain(FrontDomain childDomain) {
		this.childDomain = childDomain;
	}

	public FrontField getParentId() {
		return parentId;
	}

	public void setParentId(FrontField parentId) {
		this.parentId = parentId;
	}

	public FrontDomain getParentDomain() {
		return parentDomain;
	}

	public void setParentDomain(FrontDomain parentDomain) {
		this.parentDomain = parentDomain;
	}

	public FrontDomain getChildDomain() {
		return childDomain;
	}

	public void setChildDomain(FrontDomain childDomain) {
		this.childDomain = childDomain;
	}

	public String getIndexSuffix() {
		return indexSuffix;
	}

	public void setIndexSuffix(String indexSuffix) {
		this.indexSuffix = indexSuffix;
	}

}
